/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.colegiatura.beans;

import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.cip.esymons.colegiatura.bc.AreaDelegate;
import org.cip.esymons.general.be.Area;

/**
 *
 * @author ADMINISTRADOR
 */
public class areaController {

    private AreaDelegate delegadoArea;
    private Area area;

    public areaController() {

        delegadoArea = new AreaDelegate();
        area = new Area();
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }

    public AreaDelegate getDelegadoArea() {
        return delegadoArea;
    }

    public void setDelegadoArea(AreaDelegate delegadoArea) {
        this.delegadoArea = delegadoArea;
    }

    public Area buscarArea(Integer codigo){

        return delegadoArea.find(codigo);
    }

    public String grabar(){

        delegadoArea.create(area);
        area = new Area();
        mostrarMensaje("INFO: ", "Área registrado satisfactoriamente...", FacesMessage.SEVERITY_INFO);
        return null;
    }

    public String editar(){

        delegadoArea.edit(area);
        area = new Area();
         mostrarMensaje("INFO: ", "Área actualizado satisfactoriamente...", FacesMessage.SEVERITY_INFO);
        return "ir_a_listar";
    }

    public String eliminar(){

        delegadoArea.remove(area);
        area = new Area();
        return "ir_a_listar";
    }

    public String nuevo(){

        area = new Area();
        return "nueva_area";
    }

    public String remover(){

        delegadoArea.remove(area);
        area = new Area();
        return null;
    }
    public List<Area> getListaAreas(){

        return delegadoArea.findAll();
    }

     public SelectItem[] getSelectListaAreas(){

      try{
//        System.out.println("Ingreso 1");
        SelectItem[] selects = null;
        List<Area> listaArea = delegadoArea.findAll();
        selects = new SelectItem[listaArea.size()];
        int cont = 0;
        for(Area are : listaArea){

            SelectItem item = new SelectItem(are, are.getAreNombre());
            selects[cont] = item;
            cont++;
        }
        return selects;
      }catch(Exception e){
            System.out.println("Ingreso Error");
            SelectItem[] selects = new SelectItem[1];
            SelectItem item = new SelectItem(null, "No existen areas");
            selects[0] = item;
            return selects;
      }

    }

   public SelectItem[] getSelectListaArea(){

      try{
//        System.out.println("Ingreso 1");
        SelectItem[] selects = null;
        List<Area> listaArea = delegadoArea.findAll();
        selects = new SelectItem[listaArea.size()];
        int cont = 0;
        for(Area are : listaArea){

            SelectItem item = new SelectItem(are.getAreCodigo(), are.getAreNombre());
            selects[cont] = item;
            cont++;
        }
        
        return selects;
      }catch(Exception e){
            System.out.println("Ingreso Error");
            SelectItem[] selects = new SelectItem[1];
            SelectItem item = new SelectItem(null, "No existen areas");
            selects[0] = item;
            return selects;
      }

    }

   public void mostrarMensaje(String resumen, String detalle, FacesMessage.Severity severidad) {
        try {
            FacesMessage fm = new FacesMessage(severidad, resumen, detalle);
            FacesContext.getCurrentInstance().addMessage(fm.toString(), fm);
        } catch (Exception e) {
            FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage());
            FacesContext.getCurrentInstance().addMessage(fm.toString(), fm);
        }
    }
}
